package love.luffa.pay.controller;

import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import com.wechat.pay.java.service.payments.nativepay.model.QueryOrderByOutTradeNoRequest;
import jakarta.servlet.http.HttpServletRequest;
import love.luffa.pay.server.WeChatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @ClassName WeChatPayController
 * @Description TODO
 * @Author kite
 * @Date 2025/2/22 15:47
 * @Version 1.0
 **/
@RestController
@RequestMapping("/weChatPay")
public class WeChatPayController {
    /**
     * 商户号
     */
    @Value("${wechat.pay.merchantId}")
    public String merchantId;

    /**
     * AppId
     */
    @Value("${wechat.pay.appId}")
    public String appId;

    @Autowired
    private NativePayService nativePayService;

    @Autowired
    private NotificationParser notificationParser;

    @RequestMapping("/test")
    public String test(HttpServletRequest request) {
        try {
            Transaction parse = notificationParser.parse(WeChatUtil.handleNodifyRequestParam(request), Transaction.class);
            System.out.println("支付通知"+parse);
            QueryOrderByOutTradeNoRequest queryOrderByOutTradeNoRequest = new QueryOrderByOutTradeNoRequest();
            queryOrderByOutTradeNoRequest.setOutTradeNo(parse.getOutTradeNo());
            queryOrderByOutTradeNoRequest.setMchid(merchantId);
            Transaction transaction = nativePayService.queryOrderByOutTradeNo(queryOrderByOutTradeNoRequest);
            System.out.println("查单结果："+transaction.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("有人支付了！");
        return "ok";
    }



    @RequestMapping("/pay")
    public String pay(@RequestParam("order") String order) {
        // request.setXxx(val)设置所需参数，具体参数可见Request定义
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        amount.setTotal(1);
        request.setAmount(amount);
        request.setAppid(appId);
        request.setMchid(merchantId);
        request.setDescription("鸿晕晕测试商品标题");
        request.setNotifyUrl("http://wwgg8c.natappfree.cc/weChatPay/test");
        request.setOutTradeNo(order);
        // 调用下单方法，得到应答
        PrepayResponse response = nativePayService.prepay(request);
        // 使用微信扫描 code_url 对应的二维码，即可体验Native支付
        String codeUrl = response.getCodeUrl();
        return codeUrl;
    }

    @GetMapping("/queryByOutTradeNo")
    public String queryByOutTradeNo(@RequestParam("outTradeNo") String outTradeNo) {
        QueryOrderByOutTradeNoRequest queryOrderByOutTradeNoRequest = new QueryOrderByOutTradeNoRequest();
        queryOrderByOutTradeNoRequest.setOutTradeNo(outTradeNo);
        queryOrderByOutTradeNoRequest.setMchid(merchantId);
        Transaction transaction = nativePayService.queryOrderByOutTradeNo(queryOrderByOutTradeNoRequest);
        System.out.println("查单结果 "+outTradeNo+"："+transaction.toString());
        return transaction.toString();
    }


}
